package com.example.demo.controller.admin;

import com.example.demo.common.CommonResult;
import com.example.demo.common.StatusCode;
import com.example.demo.domain.Order;
import com.example.demo.domain.OrderRooms;
import com.example.demo.domain.User;
import com.example.demo.service.OrderService;
import com.example.demo.service.RoomService;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2024-11-06
 * @Version: 1.0
 */
@RestController("adminOrderController")
@RequestMapping("/admin")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private RoomService roomService;
    @Resource
    private UserService userService;

    /**
     * 这种情况，改为使用pageHelpler来实现分页
     * @param flags
     * @param roomNumber
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/listOrders")
    public CommonResult<PageInfo<OrderRooms>> listOrders(@RequestParam("orderFlags") List<Integer> flags,
                                                         @RequestParam(required = false) String roomNumber,
                                                         @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "10", value = "pageSize") Integer pageSize) {


        CommonResult<PageInfo<OrderRooms>> commonResult = new CommonResult<>();

       PageInfo<OrderRooms> page = orderService.getListRoomsByPage(roomNumber,flags,pageNum,pageSize);

        //set the response code and message
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());

        //set the data
        commonResult.setData(page);

        return commonResult;
    }

    @PostMapping("/unsubscribe")
    public CommonResult<String> unsubscribe(@RequestParam("orderId") Integer orderId) {

       CommonResult<String> commonResult = new CommonResult<>();

        // 根据订单id查询订单
        Order order = orderService.getById(orderId);
        // 修改订单状态为已退订
        order.setFlag(2);
        // 更新订单
        boolean result = orderService.updateById(order);

        // 返回结果
        if (result) {
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("退订成功");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("退订失败");
        }

        return commonResult;
    }

    @PostMapping("/handle")
    public CommonResult<String> handle(@RequestParam("orderId") Integer orderId) {
        CommonResult<String> commonResult = new CommonResult<>();

        //根据订单id查询订单
        Order order = orderService.getById(orderId);
        //将订单状态改为已处理
        order.setFlag(1);
        //更新订单
        boolean result = orderService.updateById(order);

        //根据订单状态判断是否处理成功
        if (result) {
            //调用房间服务，更新房间状态
            roomService.bookRoom(order.getRoomId());
            //根据订单用户id查询用户
            User user = userService.getById(order.getUserId());
            //计算用户积分
            int jifen = (int) (user.getJifen() + order.getRealPrice());
            //更新用户积分
            user.setJifen(jifen);
            userService.updateById(user);

            //处理成功
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("办理入住成功");
        } else {
            //处理失败
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("办理入住失败");
        }

        return commonResult;
    }
}